package com.detection.yolo.service;

import com.dahuatech.hutool.core.thread.ThreadUtil;
import com.detection.common.config.DetectionConfig;
import com.detection.tcp.utils.CameraDetectionStatusUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;

@Service
@Slf4j
public class FfmpegRecordVideoService {

    public String getHlsUrl(String hlsNetPath,String m3u8Name) throws IOException, InterruptedException {

        String filePath = DetectionConfig.getProfile() + "/hls/"+m3u8Name;
        new File(filePath).mkdirs();

        ProcessBuilder pb = new ProcessBuilder(DetectionConfig.getFfmpegPath(),
                "-i",hlsNetPath,"-an",
                "-c:v","libx264",
                "-preset","veryfast",
                "-tune","zerolatency",
                "-crf","23",
                "-g","50",
                "-keyint_min","48",
                "-force_key_frames","expr:gte(t,n_forced*2)",
                "-f","hls",
                "-hls_time","2",
                "-hls_list_size","5",
                "-hls_flags","delete_segments",
                "-hls_segment_filename",filePath+"/"+m3u8Name+"_%03d.ts",
                filePath+"/"+m3u8Name+".m3u8");
        pb.redirectErrorStream(true);

        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                log.info("视频"+m3u8Name+"正在录制，录制的流地址为："+hlsNetPath);
                Process process = null;
                try {
                    process = pb.start();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                CameraDetectionStatusUtils.ffmpegRecordProcessMap.put(m3u8Name,process);
                InputStream is = process.getInputStream();
                InputStreamReader reader = new InputStreamReader(is);
                BufferedReader bufferedReader = new BufferedReader(reader);
                String line;
                try {
                    while ((line=bufferedReader.readLine())!=null){
                        log.info("[ffmpeg]"+line);
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        });


        //销毁进程
//        process.destroy();
        String sendAddress = DetectionConfig.getSendAddress();
//        Thread.sleep(5000);
        return sendAddress+"/profile/hls/"+m3u8Name+"/"+m3u8Name+".m3u8";
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        FfmpegRecordVideoService ffmpegRecordVideoService = new FfmpegRecordVideoService();
        DetectionConfig detectionConfig = new DetectionConfig();
        detectionConfig.setProfile("D:/wancheng/uploadPath");
        detectionConfig.setFfmpegPath("D:/BaiduNetdiskDownload/ffmpeg-4.3.1-2021-01-01-essentials_build/ffmpeg-4.3.1-2021-01-01-essentials_build/bin/ffmpeg.exe");
        detectionConfig.setSendAddress("http://127.0.0.1:8098");
        String netPath="https://vvip.high26-playback.com/20250727/16714_a69d15dc/2000k/hls/mixed.m3u8";
        String network = ffmpegRecordVideoService.getHlsUrl(netPath,"yfjcs");
        System.out.println(network);
    }

}
